﻿/// <reference path="utils.js" />
'use strict';

define([
    'common/models/billingHistory.model',
], function (Model) {
    return Backbone.Collection.extend({
        defaults: {
            model: Model,
        },

        fetch: function (params, options) {
            var that = this;

            if (!options) options = {};

            App.mainService.runProcedure(
                {
                    Procedure: App.config.databases.billing + '.BILL.GetBillingHistory',
                    Parameters: (params instanceof Array) ? params : utils.backend.objToQueryParams(params),
                },
                _.extend({}, options, {
                    success: function (resp) {
                        try {
                            if (resp.Success == true) {
                                if (resp.Data) {
                                    var newColl = [];

                                    _.each(resp.Data[0], function (item, i) {
                                        newColl.push(new Model({
                                            id: item.Id,
                                            datetime: item.Datetime,
                                            timestamp: item.Timestamp,
                                            customer: item.Customer,
                                            price: item.Price,
                                            paid: item.Paid,
                                            pay: item.Pay,
                                        }));
                                    });

                                    that.set(newColl);

                                    if (options.success)
                                        options.success(that);
                                }
                                else {
                                    console.debug('data null');

                                    if (options.error)
                                        options.error('data null');
                                }
                            }
                            else {
                                console.debug(resp.Message);

                                if (options.error)
                                    options.error(resp.Message);
                            }
                        }
                        catch (Error) {
                            if (options.async) {
                                console.error(Error);

                                if (options.error)
                                    options.error('Exception');
                            }
                            else {
                                throw Error;
                            }
                        }
                    },
                    error: function () {
                        console.debug('error');

                        if (options.error)
                            options.error('error');
                    },
                    //async: false,
                })
            );

            return this;
        },
    });
});